﻿using System;
//using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace prjKQXS_DLL.Dal
{
    public class PhanQuyen_DAL
    {
        SqlCommand sqlCommand = null;
        public int deletePhanQuyen(int id_rec)
        {
            int retVal = 0;
            SqlConnection conn = new SqlConnection(BienTc.connString);
            conn.Open();
            {
                sqlCommand = new SqlCommand("delete PhanQuyen where ID_REC = @id", conn);
                sqlCommand.Parameters.AddWithValue("@id", id_rec);
                retVal = sqlCommand.ExecuteNonQuery();
            }
            return retVal;
        }
        public int updatePhanQuyenByIdAndQuyen(int id, string columnName, int flag)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            {
                try
                 
                {
                    conn.Open();
                    sqlCommand = new SqlCommand("update PhanQuyen set " + columnName.Trim() + " = @flag where id = @id", conn);
                    sqlCommand.Parameters.AddWithValue("@columnName", columnName);
                    sqlCommand.Parameters.AddWithValue("@flag", flag);
                    sqlCommand.Parameters.AddWithValue("@id", id);
                    return sqlCommand.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    throw;
                }
                finally
                {
                    sqlCommand.Dispose();
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
        public int InsertPhanQuyen(int idForm, string id_rec, bool toanquyen, bool nhapsua, bool nhap, bool sua, bool xem, bool camtruycap)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            {
                conn.Open();
                sqlCommand = new SqlCommand("insert into [PhanQuyen](IdForm,ID_REC,ToanQuyen,NhapSua,Nhap,Sua,Xem,CamTruyCap) values(@IdForm,@ID_REC,@ToanQuyen,@NhapSua,@Nhap,@Sua,@Xem,@CamTruyCap)", conn);
                try
                {

                    sqlCommand.Parameters.AddWithValue("@IdForm", idForm);
                    sqlCommand.Parameters.AddWithValue("@ID_REC", id_rec);
                    sqlCommand.Parameters.AddWithValue("@ToanQuyen", toanquyen);
                    sqlCommand.Parameters.AddWithValue("@NhapSua", nhapsua);
                    sqlCommand.Parameters.AddWithValue("@Nhap", nhap);
                    sqlCommand.Parameters.AddWithValue("@Sua", sua);
                    sqlCommand.Parameters.AddWithValue("@Xem", xem);
                    sqlCommand.Parameters.AddWithValue("@CamTruyCap", camtruycap);
                    return sqlCommand.ExecuteNonQuery();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    sqlCommand.Dispose();
                    conn.Close();
                    conn.Dispose();
                }
            }
        }

        public int InsertPhanQuyen(int idForm, string id_rec, bool toanquyen, bool nhapsua, bool nhap, bool sua, bool xem, bool camtruycap, SqlConnection conn)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn = new SqlConnection(BienTc.connString);
                    conn.Open();
                sqlCommand = new SqlCommand("insert into [PhanQuyen](IdForm,ID_REC,ToanQuyen,NhapSua,Nhap,Sua,Xem,CamTruyCap) values(@IdForm,@ID_REC,@ToanQuyen,@NhapSua,@Nhap,@Sua,@Xem,@CamTruyCap)", conn);
                sqlCommand.Parameters.AddWithValue("@IdForm", idForm);
                sqlCommand.Parameters.AddWithValue("@ID_REC", id_rec);
                sqlCommand.Parameters.AddWithValue("@ToanQuyen", toanquyen);
                sqlCommand.Parameters.AddWithValue("@NhapSua", nhapsua);
                sqlCommand.Parameters.AddWithValue("@Nhap", nhap);
                sqlCommand.Parameters.AddWithValue("@Sua", sua);
                sqlCommand.Parameters.AddWithValue("@Xem", xem);
                sqlCommand.Parameters.AddWithValue("@CamTruyCap", camtruycap);
                return sqlCommand.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                sqlCommand.Dispose();
                conn.Close();
                conn.Dispose();
            }

        }
    }
}
